package o;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.text.TextUtils;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appgallery.foundation.card.base.bean.AppInfoBean;
import com.huawei.appgallery.foundation.card.base.bean.DependAppBean;
import com.huawei.appmarket.service.appmgr.view.activity.AppManagerProtocol;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import o.ewm.i;
import o.ewm.n;
import org.jetbrains.annotations.NotNull;

public class dwe {
    private Map<String, AppInfoBean> ˊ = null;
    private boolean ˋ = true;
    private dwk ˎ = null;
    private o.ejj.b ˏ;
    private dwj ॱ;

    public interface a {
        void ˎ(SessionDownloadTask sessionDownloadTask);
    }

    class b implements o.dqw.c {
        private AppInfoBean ˋ;
        final /* synthetic */ dwe ˎ;
        private a ˏ;

        public b(dwe o_dwe, AppInfoBean appInfoBean, a aVar) {
            this.ˎ = o_dwe;
            this.ˋ = appInfoBean;
            this.ˏ = aVar;
        }

        public void ˋ(SessionDownloadTask sessionDownloadTask) {
            if (sessionDownloadTask != null && this.ˋ != null) {
                this.ˎ.ˊ(sessionDownloadTask, this.ˋ);
                if (this.ˏ != null) {
                    this.ˏ.ˎ(sessionDownloadTask);
                }
            }
        }

        public void ॱ() {
            if (this.ˏ != null) {
                this.ˏ.ˎ(null);
            }
            Logger.ˋ("BatchDownloadAdapter", "BatchDownloadBundleListener get bundle fail.");
        }
    }

    class c implements o.ckh.a {
        private final Context ˊ;
        final /* synthetic */ dwe ˋ;
        private final List<SessionDownloadTask> ˏ;

        public c(dwe o_dwe, Context context, List<SessionDownloadTask> list) {
            this.ˋ = o_dwe;
            this.ˏ = list;
            this.ˊ = context;
        }

        public void ˏ(AlertDialog alertDialog, com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity.DialogActivity.a aVar, int i) {
            if (aVar.ˏ() == null) {
                Logger.e("BatchDownloadAdapter", "view is null");
            } else if (-1 == i) {
                dvr.ॱ(aVar.ˏ(), true);
                ˏ();
                for (SessionDownloadTask sessionDownloadTask : this.ˏ) {
                    r1 = this.ˋ.ˎ.ˊ(sessionDownloadTask.ˊˊ());
                    if (r1 != null) {
                        sessionDownloadTask = r1;
                    }
                    this.ˋ.ˎ.ॱ(sessionDownloadTask, true, true);
                }
                if (this.ˋ.ॱ != null) {
                    this.ˋ.ॱ.ॱ();
                }
            } else if (-2 == i) {
                dvr.ॱ(aVar.ˏ(), false);
                ˏ();
                for (SessionDownloadTask sessionDownloadTask2 : this.ˏ) {
                    r1 = this.ˋ.ˎ.ˊ(sessionDownloadTask2.ˊˊ());
                    if (r1 != null) {
                        sessionDownloadTask2 = r1;
                    }
                    this.ˋ.ˎ.ॱ(sessionDownloadTask2, false, true);
                }
                dwb.ˎ(this.ˏ.size());
                if (this.ˋ.ॱ != null) {
                    this.ˋ.ॱ.ˎ();
                }
            } else {
                Logger.e("BatchDownloadAdapter", "Invalid which:" + i);
            }
        }

        private void ˏ() {
            if (this.ˊ instanceof Activity) {
                this.ˋ.ˏ((Activity) this.ˊ);
            } else {
                Logger.e("BatchDownloadAdapter", "context is not activity");
            }
        }
    }

    static class d implements OnDismissListener {
        private dwl ˋ;

        public d(dwl o_dwl) {
            this.ˋ = o_dwl;
        }

        public void onDismiss(DialogInterface dialogInterface) {
            if (this.ˋ != null && this.ˋ.ॱ() != null) {
                this.ˋ.ॱ().ॱ();
            }
        }
    }

    class e implements o.dts.e {
        final /* synthetic */ dwe ॱ;

        private e(dwe o_dwe) {
            this.ॱ = o_dwe;
        }

        public void ˎ(Context context, int i) {
            if (i <= 0) {
                this.ॱ.ˎ(context);
            } else {
                this.ॱ.ˏ(context);
            }
        }

        public void ॱ(Context context) {
        }
    }

    private void ˏ(final Context context) {
        if (ejj.ˊ().ˎ()) {
            ejj.ˊ().ˏ(context, new ejf(this) {
                final /* synthetic */ dwe ˎ;

                public void ˋ() {
                    this.ˎ.ˋ(context);
                    if (this.ˎ.ˏ != null) {
                        this.ˎ.ˏ.ˊ();
                    }
                }

                public void ॱ() {
                    if (this.ˎ.ˏ != null) {
                        this.ˎ.ˏ.ˏ();
                    }
                }
            });
        } else {
            ˋ(context);
        }
    }

    public void ˊ(Activity activity, View view) {
        List arrayList = new ArrayList();
        for (AppInfoBean appInfoBean : this.ˊ.values()) {
            if (TextUtils.isEmpty(appInfoBean.ˏॱ())) {
                Logger.e("BatchDownloadAdapter", "can not find pkg");
            } else {
                o.dts.b bVar = new o.dts.b();
                bVar.ˊ(appInfoBean.ʽ());
                bVar.ˏ(appInfoBean.ˏॱ());
                arrayList.add(bVar);
            }
        }
        dts o_dts = new dts(arrayList, new e());
        o_dts.ˊ(view);
        o_dts.ˋ((Context) activity);
    }

    private void ˋ(final Context context) {
        try {
            if (!ॱ()) {
                return;
            }
            if (doo.ˊ(context)) {
                this.ˎ = new dwk();
                if (dvr.ˏ(context, true)) {
                    if (!CollectCheck.isEmpty(this.ˊ)) {
                        for (AppInfoBean appInfoBean : this.ˊ.values()) {
                            if (appInfoBean != null) {
                                ॱ(appInfoBean, new a(this) {
                                    final /* synthetic */ dwe ˋ;

                                    public void ˎ(SessionDownloadTask sessionDownloadTask) {
                                        if (sessionDownloadTask != null) {
                                            if (context instanceof Activity) {
                                                sessionDownloadTask.ˊ(dgm.ˎ((Activity) context));
                                            }
                                            this.ˋ.ˎ.ˏ(sessionDownloadTask);
                                        }
                                    }
                                });
                            }
                        }
                    }
                    if (context instanceof Activity) {
                        ˏ((Activity) context);
                    } else {
                        Logger.e("BatchDownloadAdapter", "context is not activity");
                    }
                    if (this.ॱ != null) {
                        this.ॱ.ॱ();
                        return;
                    }
                    return;
                }
                ॱ(context);
                return;
            }
            evk.ˏ(context, n.ʹˊ, 0).ˋ();
        } catch (Exception e) {
            Logger.e("BatchDownloadAdapter", "download(Context context) " + e.toString());
        }
    }

    private boolean ॱ() {
        if (!CollectCheck.isEmpty(this.ˊ)) {
            return true;
        }
        Logger.e("BatchDownloadAdapter", "mAppInfoList is NULL");
        return false;
    }

    private boolean ˏ(SessionDownloadTask sessionDownloadTask, Context context) {
        if (sessionDownloadTask != null && !dof.ʽ(sessionDownloadTask.ˊˊ())) {
            return true;
        }
        String str;
        evk.ˏ(context, n.ʾˋ, 0).ˋ();
        String str2 = "BatchDownloadAdapter";
        StringBuilder append = new StringBuilder().append("download fail :  downloadTask = ").append(sessionDownloadTask).append(" downloadTask.getPackageName_()=");
        if (sessionDownloadTask == null) {
            str = null;
        } else {
            str = sessionDownloadTask.ˊˊ();
        }
        Logger.e(str2, append.append(str).append(" context=").append(context).toString());
        return false;
    }

    private void ॱ(final Context context) {
        dwf.ˏ(this.ˊ, new o.dwf.a(this) {
            final /* synthetic */ dwe ˋ;

            public void ˊ(@Nullable List<SessionDownloadTask> list) {
                if (CollectCheck.isEmpty((List) list)) {
                    Logger.e("BatchDownloadAdapter", "downloadTaskList is null");
                    return;
                }
                List ˎ = this.ˋ.ˋ((List) list, context);
                if (CollectCheck.isEmpty(ˎ)) {
                    Logger.e("BatchDownloadAdapter", "downloadTaskList is null");
                    return;
                }
                long ˏ = dvr.ˏ(ˎ);
                dwl o_dwl = new dwl();
                o.ckh.a cVar = new c(this.ˋ, context, ˎ);
                OnDismissListener dVar = new d(o_dwl);
                if (dvr.ˏ(context)) {
                    dvr.ˋ(context, ˏ, cVar, dVar);
                } else if (dvr.ॱ(context)) {
                    this.ˋ.ˊ(context);
                } else if (dvr.ˋ(context)) {
                    dvr.ˏ(context, ˏ, cVar, dVar);
                } else {
                    Logger.e("BatchDownloadAdapter", "Invalid network status");
                }
            }
        });
    }

    @NotNull
    private List<SessionDownloadTask> ˋ(@NotNull List<SessionDownloadTask> list, Context context) {
        List<SessionDownloadTask> arrayList = new ArrayList();
        for (SessionDownloadTask sessionDownloadTask : list) {
            if (context instanceof Activity) {
                sessionDownloadTask.ˊ(dgm.ˎ((Activity) context));
            }
            switch (((cdg) cdb.ॱ(cdg.class)).ˋ(sessionDownloadTask.ˊˊ())) {
                case 0:
                case 11:
                    break;
                case 1:
                case 2:
                    dsl.ˊ(sessionDownloadTask.ˊˊ(), sessionDownloadTask.ʿ(), sessionDownloadTask.ˎˎ(), 0, coc.NORMAL);
                    break;
                default:
                    AppInfoBean appInfoBean = (AppInfoBean) this.ˊ.get(sessionDownloadTask.ˊˊ());
                    if (appInfoBean == null) {
                        Logger.ˋ("BatchDownloadAdapter", "appInfoBean is null");
                        break;
                    }
                    ˊ(sessionDownloadTask, appInfoBean);
                    if (!ˏ(sessionDownloadTask, context)) {
                        break;
                    }
                    arrayList.add(sessionDownloadTask);
                    break;
            }
        }
        return arrayList;
    }

    private void ˊ(final Context context) {
        for (final AppInfoBean appInfoBean : this.ˊ.values()) {
            if (appInfoBean != null) {
                ॱ(appInfoBean, new a(this) {
                    final /* synthetic */ dwe ॱ;

                    public void ˎ(final SessionDownloadTask sessionDownloadTask) {
                        if (sessionDownloadTask != null) {
                            if (context instanceof Activity) {
                                sessionDownloadTask.ˊ(dgm.ˎ((Activity) context));
                            }
                            if (this.ॱ.ˏ(sessionDownloadTask, context)) {
                                List ʻॱ = appInfoBean.ʻॱ();
                                if (CollectCheck.isEmpty(ʻॱ)) {
                                    this.ॱ.ˎ.ˊ(sessionDownloadTask, true);
                                } else {
                                    djz.ˏ(ʻॱ, new o.djz.c(this) {
                                        final /* synthetic */ AnonymousClass4 ˋ;

                                        public void ॱ(@Nullable List<o.djz.b> list) {
                                            if (!CollectCheck.isEmpty((List) list)) {
                                                List arrayList = new ArrayList();
                                                for (o.djz.b bVar : list) {
                                                    SessionDownloadTask ˋ = bVar.ˋ();
                                                    DependAppBean ॱ = bVar.ॱ();
                                                    if (!(ˋ == null || ॱ == null)) {
                                                        this.ˋ.ॱ.ˊ(ˋ, ॱ.ˎ());
                                                        arrayList.add(ˋ);
                                                    }
                                                }
                                                sessionDownloadTask.ॱ(arrayList);
                                                this.ˋ.ॱ.ˎ.ˊ(sessionDownloadTask, true);
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    }
                });
            }
        }
        dwb.ˎ(this.ˊ.size());
        if (this.ॱ != null) {
            this.ॱ.ॱ();
        }
        if (context instanceof Activity) {
            ˏ((Activity) context);
        } else {
            Logger.e("BatchDownloadAdapter", "context is not activity");
        }
    }

    private void ˎ(Context context) {
        cku ˏ = cku.ˏ(context, cku.class, null, context.getResources().getQuantityString(i.ˌ, this.ˊ.size(), new Object[]{Integer.valueOf(this.ˊ.size())}));
        ˏ.ˋ(new ewa(this) {
            final /* synthetic */ dwe ˊ;

            {
                this.ˊ = r1;
            }

            public void ˏ(Activity activity, DialogInterface dialogInterface, int i) {
                if (i == -1) {
                    this.ˊ.ˏ((Context) activity);
                }
            }

            public void ˊ(Activity activity, DialogInterface dialogInterface) {
            }
        });
        ˏ.ˎ(context);
    }

    public void ˏ(AppInfoBean appInfoBean) {
        if (appInfoBean != null) {
            if (this.ˊ == null) {
                this.ˊ = new HashMap();
            }
            this.ˊ.put(appInfoBean.ˏॱ(), appInfoBean);
        }
    }

    public void ˋ(Map<String, AppInfoBean> map) {
        this.ˊ = map;
    }

    private void ˏ(Activity activity) {
        if (this.ˋ) {
            try {
                cjk.ˎ().ˎ(activity, new AppManagerProtocol().ˎ(), 1002);
            } catch (ActivityNotFoundException e) {
                Logger.ˋ("BatchDownloadAdapter", "ActivityNotFoundException :" + e.toString());
            }
        }
    }

    public void ˊ(boolean z) {
        this.ˋ = z;
    }

    private void ॱ(AppInfoBean appInfoBean, a aVar) {
        int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(appInfoBean.ˏॱ());
        switch (ˋ) {
            case 0:
            case 11:
                if (aVar != null) {
                    aVar.ˎ(null);
                    break;
                }
                break;
            case 1:
            case 2:
                dsl.ˊ(appInfoBean.ˏॱ(), appInfoBean.ʽ(), appInfoBean.ˋॱ(), 0, coc.NORMAL);
                if (aVar != null) {
                    aVar.ˎ(null);
                    break;
                }
                break;
            default:
                ˋ(appInfoBean, aVar);
                break;
        }
        Logger.ˎ("BatchDownloadAdapter", " getDownloadTaskAsync state:" + ˋ);
    }

    private void ˋ(AppInfoBean appInfoBean, a aVar) {
        new dqw().ˏ(dqw.ˊ(appInfoBean.ˏॱ(), appInfoBean.ˈ(), appInfoBean.ʻ()), new b(this, appInfoBean, aVar));
    }

    private void ˊ(@NonNull SessionDownloadTask sessionDownloadTask, @NonNull AppInfoBean appInfoBean) {
        sessionDownloadTask.ʼ(appInfoBean.ˏॱ());
        if (appInfoBean.ˈ() == 0) {
            SplitTask splitTask = new SplitTask();
            splitTask.ˋ(appInfoBean.ͺ());
            splitTask.ˊ(appInfoBean.ˊॱ());
            splitTask.ˊॱ(appInfoBean.ˏॱ());
            try {
                splitTask.ˋ(Long.parseLong(appInfoBean.ॱˎ()));
            } catch (Throwable e) {
                Logger.ˋ("BatchDownloadAdapter", "can not get fileSize", e);
            }
            sessionDownloadTask.ˏ(splitTask);
        }
        sessionDownloadTask.ˊ(appInfoBean.ͺ());
        sessionDownloadTask.ॱॱ(appInfoBean.ʽ());
        sessionDownloadTask.ʼ(appInfoBean.ˏॱ());
        sessionDownloadTask.ॱ(appInfoBean.ᐝ());
        sessionDownloadTask.ʽ(appInfoBean.ˋॱ());
        sessionDownloadTask.ˏ(appInfoBean.ॱˊ());
        sessionDownloadTask.ॱ(appInfoBean.ॱॱ());
        sessionDownloadTask.ॱˊ("familyShare=" + appInfoBean.ˎ());
        sessionDownloadTask.ॱˊ(appInfoBean.ˈ());
        try {
            sessionDownloadTask.ʻ(Integer.parseInt(appInfoBean.ʻ()));
        } catch (Throwable e2) {
            Logger.ˋ("BatchDownloadAdapter", "can not get versionCode!!!!!", e2);
        }
        if (((cdg) cdb.ॱ(cdg.class)).ˋ(appInfoBean.ˏॱ()) == 4) {
            ApkUpgradeInfo ॱ = dug.ॱ(appInfoBean.ˏॱ());
            if (ॱ != null) {
                List ᐝˊ = sessionDownloadTask.ᐝˊ();
                if (!CollectCheck.isEmpty(ᐝˊ)) {
                    SplitTask splitTask2 = (SplitTask) ᐝˊ.get(0);
                    splitTask2.ˋ(ॱ.ʽॱ());
                    splitTask2.ˊ(ॱ.ॱॱ());
                    splitTask2.ˋ(ॱ.ʻॱ());
                    splitTask2.ॱ(ॱ.ʼ());
                    splitTask2.ˏ(ॱ.ॱॱ());
                    splitTask2.ˎ(ॱ.ˈ());
                }
                sessionDownloadTask.ˊ(ॱ.ʽॱ());
                sessionDownloadTask.ॱ(ॱ.ˏˎ());
                sessionDownloadTask.ˏ(ॱ.ˊᐝ());
            }
        }
    }

    public void ˎ(dwj o_dwj) {
        this.ॱ = o_dwj;
    }
}
